<template>
	<!-- 全真模拟 -->
	<view class="simulation pd-30">
		<u-navbar back-icon-color="#fff" title-color="#fff" title="我的试卷" :border-bottom="false"></u-navbar>

		<!-- <view class="paper-list" v-if="!emptyFalse">
			<view class="list">
				<my-paper-list-item @click.native="review(paper.paperid)" @start-exam="handleStart" :item="paper" v-for="paper in list" :key="paper.paperid"></my-paper-list-item>
			</view>
		</view> -->
		<uni-swipe-action class="paper-list" v-if="!emptyFalse">
			<uni-swipe-action-item v-for="(paper,index) in moniPaper.list" :key="paper.paperid" :show="paper.show" :options="options" @click="click(paper,index)" @open="open">
				<view class="list">
					<my-paper-list-item @click.native="review(paper.paperid)" @start-exam="handleStart" :item="paper"></my-paper-list-item>
				</view>
			</uni-swipe-action-item>		
		</uni-swipe-action>	
		<my-empty v-else emptyTit="暂无试卷"></my-empty>
		<u-loadmore icon-type="flower" v-show="list.length || status === 'loading'" bg-color="#f5f5f5" :status="status" @loadmore="getPageData()" :margin-top="20" />
		
		<my-confirm ref="headBack" @cancel="backCancel()" @confirm="conback()" confirm-text="确定" cancel-text="取消">您确定删除考试吗？</my-confirm>
	</view>
</template>

<script>
import exam from '../../mixins/exam.js';
/**
 * mode 3-全真模拟 4-组卷
 * */
export default {
	mixins: [exam],
	data() {
		return {
			show: false,
			options: [
				{text: '删除',
				style: {backgroundColor: '#dd524d'},
			  }
			],
			emptyFlase:false,
			paperid:'',//选择的考试
			choseIndex:'',
			moniPaper: {
				list: [],
				pageNo: 1,
				isLastPage: false
			},
			onlinePaper: [],
			status: 'loadmore'
		};
	},
	computed: {
		list() {
			return this.moniPaper.list.length ? this.moniPaper.list : [];
		},
		emptyFalse() {
			return this.list.length > 0 ? false : true;
		},
		isLastPage() {
			return this.moniPaper.isLastPage;
		}
	},
	onLoad() {
		this.init();
	},
	onShow() {
		if (uni.getStorageSync('chapterInfo')) {
			uni.removeStorage({
				key: 'chapterInfo'
			});
		}
	},
	onReachBottom() {
		!this.moniPaper.isLastPage && this.getPageData();
	},
	methods: {
		click(data,index) {
			this.$refs.headBack.open();
			this.paperid= data.paperid
			this.choseIndex = index
			console.log(this.paperid,this.choseIndex)
		},
		// 如果打开一个的时候，不需要关闭其他，则无需实现本方法
		open(index) {
			// 先将正在被操作的swipeAction标记为打开状态，否则由于props的特性限制，
			// 原本为'false'，再次设置为'false'会无效
			this.moniPaper.list[index].show = true;
			this.moniPaper.list.map((val, idx) => {
					if(index != idx) this.moniPaper.list[idx].show = false;
			})
		},
		backCancel(){
			 this.$refs.headBack.close()
		},
		conback() {
			const paperid=this.paperid
			const choseIndex=this.choseIndex
			return new Promise((resolve, reject) => {
				this.request
					.delete('/app/teacher/papermanage/deletePaper/'+paperid,{'paperid':paperid})
					.then(res => {
					//模拟考试
				 	 this.moniPaper.list.splice(choseIndex, 1);
					 this.emptyFlase=this.moniPaper.list.length>0?false:true
					 this.$u.toast(`删除成功`);
					 this.$refs.headBack.close()
						resolve();
					})
			});
		},
		init() {
			this.moniPaper.pageNo = 1;
			this.moniPaper.list = [];
			this.moniPaper.isLastPage = false;
			this.status = 'loadmore';
			return this.getPageData();
		},
		getPageData() {
			this.status = 'loading';
			const { api, activeTab, moniPaper } = this;
			return new Promise((resolve, reject) => {
				this.request
					.post('/app/teacher/papermanage/getTeacherPaper', {
						pageNo: moniPaper.pageNo
					})
					.then(res => {
						//模拟考试
						this.moniPaper.list = moniPaper.list.concat(res.list);
						this.moniPaper.isLastPage = res.isLastPage;
						if (res.isLastPage) this.status = 'nomore';
						else this.moniPaper.pageNo++;
						resolve();
					})
					.finally(() => {
						!this.moniPaper.isLastPage && (this.status = 'loadmore');
					});
			});
		},
		readmore() {
			uni.navigateTo({
				url: '../onlinePaper/onlinePaper'
			});
		},
		createPaper(type) {
			uni.navigateTo({
				url: `../createPaper/createPaper?groupType=${type}`
			});
		},
		handleStart(paper) {
			this.activeTab === 1 ? this.startUserPaper(paper) : this.startMoni(paper);
		},
		review(paperid) {
			const apiSetting = { method: 'get', url: `/app/teacher/papermanage/getPaperQuestion/${paperid}` };
			const answerPageSetting = {
				correcting: true,
				clearHistoryHandler: 'clearPracticeHistory',
				correctTopic: true,
				initArguments: [true]
			};
			answerPageSetting.autoJump = null;
			answerPageSetting.isRecitation = true;
			answerPageSetting.clearHistory = false;
			answerPageSetting.navTitle = '试卷题目';
			answerPageSetting.updateHistoryHandler = 'updateRecitationHistory';
			this.startAnswer(apiSetting, answerPageSetting);
		}
	}
};
</script>

<style scoped lang="scss">
.simulation {
	display: flex;
	flex-direction: column;
	> view {
		position: relative;
	}
}
.list{
	width: 100%;
}
.empty {
	margin-top: 50%;
}
</style>
